package lee.vioson.ebook.ui.components.dialog

import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.itemsIndexed
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.compose.ui.window.Dialog
import lee.vioson.composecommon.widgets.MyButton
import lee.vioson.ebook.R
import lee.vioson.ebook.ui.AppTheme

/**
 *Author:viosonlee
 *Date:2021/10/14
 *DESCRIPTION: 选择用户的弹窗
 */
@Composable
fun <T> SelectDialog(
    data: List<T>,
    showText: (T) -> String,
    onDismiss: () -> Unit,
    title: String? = null,
    select: T? = null,
    onConfirm: (selectIndex: Int) -> Unit = {}
) {

    val selectedIndex = data.indexOf(select)

    Dialog(onDismissRequest = { onDismiss() }) {
        DialogContent(
            data = data,
            showText = showText,
            title = title,
            initSelectedIndex = selectedIndex,
            onConfirm = onConfirm,
            onDismiss = { onDismiss() }
        )
    }
}


@Composable
private fun <T> DialogContent(
    data: List<T>,
    showText: (T) -> String,
    title: String? = null,
    initSelectedIndex: Int = -1,
    onDismiss: () -> Unit = {},
    onConfirm: (selectIndex: Int) -> Unit = {}
) {
    var selectedIndex by remember { mutableStateOf(initSelectedIndex) }
    Column(
        Modifier.background(color = AppTheme.colors.background, shape = RoundedCornerShape(8.dp))
    ) {
        Box(
            contentAlignment = Alignment.CenterEnd,
            modifier = Modifier.fillMaxWidth()
        ) {
            Image(
                painter = painterResource(id = R.mipmap.ic_close),
                contentDescription = null,
                modifier = Modifier
                    .padding(top = 12.dp, end = 12.dp)
                    .width(24.dp)
                    .height(24.dp)
                    .clickable { onDismiss() }
            )
        }
        Text(
            text = title ?: stringResource(id = R.string.pls_select),
            color = AppTheme.colors.onBackground, fontSize = 22.sp, fontWeight = FontWeight.Medium,
            modifier = Modifier
                .padding(start = 24.dp, end = 24.dp, bottom = 4.dp)
                .fillMaxWidth()
        )
        Box(modifier = Modifier.fillMaxWidth(), contentAlignment = Alignment.BottomCenter) {
            LazyColumn(
                modifier = Modifier
                    .fillMaxWidth()
                    .padding(start = 24.dp, end = 24.dp, top = 12.dp, bottom = 96.dp)
            ) {
                itemsIndexed(data) { index, item ->
                    val isSelected = selectedIndex == index
                    Row(
                        verticalAlignment = Alignment.CenterVertically,
                        modifier = Modifier
                            .padding(top = 12.dp)
                            .fillMaxWidth()
                            .border(
                                width = 1.dp,
                                color = if (isSelected) Color(0xFF999999) else Color(0xFF666666),
                                shape = RoundedCornerShape(4.dp)
                            )
                            .background(
                                color = if (isSelected) Color(0xFF666666) else Color(0xFF333333),
                                shape = RoundedCornerShape(4.dp)
                            )
                            .clickable { selectedIndex = index }
                    ) {
                        Text(
                            text = showText(item),
                            color = AppTheme.appColors.whiteText80,
                            fontSize = 16.sp,
                            modifier = Modifier
                                .padding(start = 10.dp)
                                .weight(1f)
                                .padding(top = 12.dp, bottom = 12.dp)
                        )
                        if (isSelected) {
                            Image(
                                painter = painterResource(id = R.mipmap.icon_selected),
                                contentDescription = null,
                                modifier = Modifier.padding(end = 16.dp),
                            )
                        }
                    }
                }
            }

            MyButton(
                onClick = {
                    onDismiss()
                    onConfirm.invoke(selectedIndex)
                },
                modifier = Modifier
                    .padding(24.dp)
                    .fillMaxWidth()
                    .height(48.dp),
                text = stringResource(id = R.string.confirm),
            )
        }

    }
}

@Preview(name = "内容")
@Composable
private fun PreviewContent() {
    AppTheme {
        DialogContent(
            data = arrayListOf("type", "desc"),
            showText = { it },
            initSelectedIndex = -1
        )
    }
}
